﻿using Framework.Models.Entities;
using Framework.Models.Request;
using Framework.Repository;
using Framework.Utility.ViewModels;
using Flow.Manage.Contracts;
using Framework.Utility.Helper;

namespace Flow.Manage.Service
{
    public class AccountService : IAccountContract
    {
        private IUserRepository _adminRepository; 

        public AccountService(IUserRepository adminRepository)
        {
            this._adminRepository = adminRepository; 
        }
       /// <summary>
       /// 管理员登录
       /// </summary>
       /// <param name="dto"></param>
       /// <returns></returns>
        public TData<Biz_User> AdminLogin(AdminRequest dto)
        {
            TData<Biz_User> tData = new TData<Biz_User>(); 

            var info =  _adminRepository.GetFirstOrDefault(c => c.Password == dto.Password.Trim() && c.UserName == dto.UserName.Trim());

            if (info != null)
            {
                tData.data = info;
                tData.message = ("登录成功"); 
            }
            else
            {
                tData.tag = ResultTag.fail;
                tData.message = "登录失败";
          
            }
            return tData;
        }
        public TData AddUser(Biz_UserRequest dto)
        {  
            var entity = dto.MapTo<Biz_User>();
            entity.Id = SnowflakeId.Id; 
            var count = _adminRepository.Insert(entity); 
            return count > 0 ? new TData(ResultTag.success) : new TData(ResultTag.fail);          
        }
    }
}
